<template lang='pug'>
.timeheader(v-if='show',v-show="show_timeTitle")
  a(:href="data.linkUrl || 'javascript:;'")
    wangClocker(:limitTime="limit_time",:title="title",:difTime="difTime",@timeEnd="timeEnd")
    .limit_introduce2
</template>
<script>
import { getServiceTime } from '@/service/getData'
import { mapGetters } from 'vuex'
import wangClocker from './_wang_clocker'
export default {
  data() {
    return {
      limit_time: '',
      link_href:'',
      data:{},
      title: '',
      showclocker: false,
      show: true,
      difTime: 0,
    }
  },
  components: {
    wangClocker
  },
  props: ['cdata'],
  created() {
    if (!this.cdata[0].tag) {
      this.show = false
      return
    }
    this.setLimitTime()
  },
  methods: {
    // 设置时间
    async setLimitTime() {
      if (this.cdata[0].tag.length ==0) {
        this.$store.commit('setShowFlashSale',-1)
        return
      }
      let serviceTime = await getServiceTime();
      if (serviceTime.code != '1') {
        this.$store.commit('setShowFlashSale',-1)
        return
      }
      let time_now = parseInt(serviceTime.currentTime)
      let now = new Date().getTime()
      this.difTime = now - time_now
      this.checkTime(time_now)
    },
    // 到期
    timeEnd () {
      this.show = false
      this.$store.commit('setShowFlashSale',-1)
    },
    checkTime(time_now) {
      let data = this.getData(1);
      if (data) {
        if (time_now < new Date(data.productSpecialFlag.replace(/-/g, "/")).getTime()){
          this.$store.commit('setShowFlashSale',1)
          this.limit_time = data.productSpecialFlag.replace(/-/g, "/")
          this.title = data.elementName
          return
        }
      }
     
      data = this.getData(2)
      if (data) {
        if (time_now < new Date(data.productSpecialFlag.replace(/-/g, "/")).getTime()){
          this.$store.commit('setShowFlashSale',2)
          this.limit_time = data.productSpecialFlag.replace(/-/g, "/")
          this.title = data.elementName
          return
        }
      }
      this.$store.commit('setShowFlashSale',-1)
      this.show = false
    },
    getData(index) { 
      let a
      this.cdata[0].tag.forEach(function(item) {
        if (item.sequence == index) {
          a = item 
        }
      },this)  
      return a 
    },
  },
  computed: {
    ...mapGetters([
      'show_flashSale','show_timeTitle'
    ])
  }
}
</script>
<style lang='scss' scoped>
@import './tool.scss';
.timeheader {
  height: to_rem(110px);
  text-align: center;
  margin-top: to_rem(16px);
  background-color: #fff;
  padding-top: to_rem(20px);
  position: relative;
  
  .limit_introduce2 {
    width: toRem(750);
    height: toRem(10);
    position: absolute;
    top: toRem(72);
    left: 0;
    background: url(../images/xsmsg.png);
    background-size: 100% 100%;
  }
}
</style>